var prefix = "/standard/unit";
var regisTypeList=[];
var organTypeList=[];
var situationList=[];
var tendencyList=[];
var projectList=[];
var channelList=[];

$().ready(function() {
	getRegisType("REGISTER_TYPE");
	getOrganType("ORGAN_TYPE");
	getSituationType("UNIT_MANAGE_SITIUATION");
	getTendencyType("UNIT_MANAGE_TENDENCY");
	getProjectType("MANAGE_PROJECT");
	getChannelType("UNIT_MANAGE_CHANNEL");
	/*getBigKindType();
	getMiddleKindType();*/
	getSmallKindType();
	getDomainKindType();
	validateRule();
});


function getRegisType(type) {
	var html="";
	$.ajax({
		type: "GET",
		url: prefix+"/dict",
		data:{"type":type},
		async:false,//
		success: function(data){
			regisTypeList=data;
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].value + '">' + data[i].name + '</option>';
			}
			$(".chosen-registerType").append(html);
			$(".chosen-registerType").chosen({
				maxHeight: 200,
				width:"100%",
				disable_search_threshold:15
			});
		}
	});
}

function getOrganType(type) {
	var html="";
	$.ajax({
		type: "GET",
		url: prefix+"/dict",
		data:{"type":type},
		async:false,//
		success: function(data){
			organTypeList=data;
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].value + '">' + data[i].name + '</option>';
			}
			$(".chosen-organType").append(html);
			$(".chosen-organType").chosen({
				maxHeight: 200,
				width:"100%",
				disable_search_threshold:15
			});
		}
	});
}

function getSituationType(type) {
	var html="";
	$.ajax({
		type: "GET",
		url: prefix+"/dict",
		data:{"type":type},
		async:false,//
		success: function(data){
			situationList=data;
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].value + '">' + data[i].name + '</option>';
			}
			$(".chosen-manageSituation").append(html);
			$(".chosen-manageSituation").chosen({
				maxHeight: 200,
				width:"100%",
				disable_search_threshold:15
			});
		}
	});
}

function getTendencyType(type) {
	var html="";
	$.ajax({
		type: "GET",
		url: prefix+"/dict",
		data:{"type":type},
		async:false,//
		success: function(data){
			tendencyList=data;
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].value + '">' + data[i].name + '</option>';
			}
			$(".chosen-manageTendency").append(html);
			$(".chosen-manageTendency").chosen({
				maxHeight: 200,
				width:"100%",
				disable_search_threshold:15
			});
		}
	});
}

function getProjectType(type) {
	var html="";
	$.ajax({
		type: "GET",
		url: prefix+"/dict",
		data:{"type":type},
		async:false,//
		success: function(data){
			projectList=data;
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].value + '">' + data[i].name + '</option>';
			}
			$("#manageProject_select").append(html);

		}
	});
}

function getChannelType(type) {
	var html="";
	$.ajax({
		type: "GET",
		url: prefix+"/dict",
		data:{"type":type},
		async:false,//
		success: function(data){
			channelList=data;
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].value + '">' + data[i].name + '</option>';
			}
			$("#manageChannel_select").append(html);
		}
	});
}

function getBigKindType() {
	var html="";
	$.ajax({
		type: "GET",
		url: "/standard/industry/firstList",
		async:false,//
		success: function(data){
			channelList=data;
			if(data.code == 0)
			for (var i = 0; i < data.data.length; i++) {
				html += '<option value="' + data.data[i].bigKindCode + '">' + data.data[i].bigKindName + '</option>';
			}
			$("#industryCategoryBig").append(html);

		}
	});
}

function getMiddleKindType() {
	var html="";
	var code = $('#industryCategoryBig').val();
	if(code > 0)
	$.ajax({
		type: "GET",
		url: "/standard/industry/secondList",
		data:{"bigKindCode":code},
		async:false,//
		success: function(data){
			channelList=data;
			if(data.code == 0)
			for (var i = 0; i < data.data.length; i++) {
				html += '<option value="' + data.data[i].middleKindCode + '">' + data.data[i].middleKindName + '</option>';
			}
			$("#industryCategoryMiddle").empty();
			$("#industryCategoryMiddle").append(html);

		}
	});
}

function getSmallKindType() {
	var html="";
	/*var code = $('#industryCategoryMiddle').val();
	if(code > 0)*/
	$.ajax({
		type: "GET",
		url: "/standard/industry/thirdList",
		/*data:{"middleKindCode":code},*/
		async:false,//
		success: function(data){
			channelList=data;
			if(data.code == 0)
			for (var i = 0; i < data.data.length; i++) {
				html += '<option value="' + data.data[i].smallKindCode + '">' + data.data[i].smallKindName + '</option>';
			}
			$("#industryCategory").empty();
			$("#industryCategory").append(html);
		}
	});
}

function getDomainKindType() {
	var html='<option value="0">请选择</option>';
	var code = $('#industryCategory').val();
	if(code >0 )
	$.ajax({
		type: "GET",
		url: "/standard/reditionary/selectList",
		data:{"smallKindCode":code,"status":0},
		async:false,//
		success: function(data){
			channelList=data;
			if(data.code == 0)
			for (var i = 0; i < data.data.length; i++) {
				html += '<option value="' + data.data[i].domainKindCode + '">' + data.data[i].domainKindName + '</option>';
			}
			$("#accommodationsCategory").empty();
			$("#accommodationsCategory").append(html);
			$("#accommodationsCategory").selectpicker('refresh');

		}
	});
}

function getDictType(type,list) {
	var html="";
	$.ajax({
		type: "GET",
		url: prefix+"/dict",
		data:{"type":type},
		async:false,//
		success: function(data){
			list=data;
		}
	});
}


$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$("#manageProject").val($('#manageProject_select').selectpicker('val'));
	$("#manageChannel").val($('#manageChannel_select').selectpicker('val'));
	$.ajax({
		cache : true,
		type : "POST",
		url : "/standard/unit/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}